import java.io.File
import scala.collection.mutable.Map
import scala.io.Source
//TODO 分数类型要改为double
case class averageScore(var sum:Double, var num:Int, var aver:Double)
case class t9(var H1:Double,var L1:Double,var A1:averageScore,
              var H2:Double,var L2:Double,var A2:averageScore,
              var H3:Double,var L3:Double,var A3:averageScore)

object wordcount {
  def dealWithFirstLine(line: String, results: Map[String, t9]) = {
    val words = line.split("\\s+")//正则表达式，匹配一个或者多个空格
    var lessons = List.empty[String]
    if (words.length >=3) {
      for (i <- 2 to (words.length - 1)) {
        lessons = lessons :+ (words(i))
        if (results contains(words(i))) {}//TODO 要去掉
        else{
          results(words(i)) = t9(0, Int.MaxValue, averageScore(0, 0, 0.0),
            0, Int.MaxValue, averageScore(0, 0, 0.0),
            0, Int.MaxValue, averageScore(0, 0, 0.0))
        }
      }
    }
    (results, lessons)
  }
  def dealWithData(line: String, lessons:List[String], results: Map[String, t9]) = {
    var words = line.split("\\s+")
    if (words.length >=3) {
      for (i <- 2 to (words.length - 1)) {//TODO 代码重构
        //words(i)为分数，lessons（i-2）为科目
        //调整数值
        //最大值
        if (results(lessons(i-2)).H1 < words(i).toDouble) {results(lessons(i-2)).H1 = words(i).toDouble}
        //最小值
        if (results(lessons(i-2)).L1 > words(i).toDouble) {results(lessons(i-2)).L1 = words(i).toDouble}
        //平均值
        results(lessons(i-2)).A1.sum += words(i).toDouble
        results(lessons(i-2)).A1.num += 1
        results(lessons(i-2)).A1.aver = results(lessons(i-2)).A1.sum / results(lessons(i-2)).A1.num

        if (words(1) == "male") {
          if (results(lessons(i-2)).H2 < words(i).toDouble) {results(lessons(i-2)).H2 = words(i).toDouble}
          if (results(lessons(i-2)).L2 > words(i).toDouble) {results(lessons(i-2)).L2 = words(i).toDouble}
          //平均值
          results(lessons(i-2)).A2.sum += words(i).toDouble
          results(lessons(i-2)).A2.num += 1
          results(lessons(i-2)).A2.aver = results(lessons(i-2)).A2.sum / results(lessons(i-2)).A2.num
        } else if (words(1) == "female") {
          if (results(lessons(i-2)).H3 < words(i).toDouble) {results(lessons(i-2)).H3 = words(i).toDouble}
          if (results(lessons(i-2)).L3 > words(i).toDouble) {results(lessons(i-2)).L3 = words(i).toDouble}
          //平均值
          results(lessons(i-2)).A3.sum += words(i).toDouble
          results(lessons(i-2)).A3.num += 1
          results(lessons(i-2)).A3.aver = results(lessons(i-2)).A3.sum / results(lessons(i-2)).A3.num
        } else {}//TODO 加提示信息，告诉别人此行数据不符合规范
      }
    }
    results
  }
  def main(args: Array[String]) {
    val dirfile = new File("word/xscjtj/") //得到testfiles文件夹里面的所有文件对象构成的数组
    val files = dirfile.listFiles
    var results = Map.empty[String, t9]
    for (file <- files) {
      val data = Source.fromFile(file)
      val strs = data.getLines()
      var firstline = ""
      //文件第一行的处理
      if (strs.hasNext) firstline = strs.next()
      var lessons = List.empty[String]
      val res = dealWithFirstLine(firstline, results)
      results = res._1
      lessons = res._2

      //第二行开始
      while (strs.hasNext) {
        results = dealWithData(strs.next(), lessons, results)
      }
    }
    results foreach println//TODO 输出更加易读
  }
}
